﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace IronWinter
{
    public class Player
    {
        int m_designate;
        List<Unit> controllingUnits; //All the units that are owned by the player
        List<Building> controllingBuildings; //All the buildings that are owned by the player
        int m_requisition; //the resource of the player
        public List<UnitSpecs> unitSpecs;
        int m_score;
        int unitSpawn;
        int buildingSpawn;
        public HQBuilding hq;

        protected TimeSpan lastResTick;
        int reqValue; 

        public Color color;

        
        public void Initialize(int designate)
        {
            lastResTick = TimeSpan.MinValue;
            reqValue = 5;
            unitSpawn = designate * 1000 + 1;
            buildingSpawn = designate * 100 + 1;
            controllingBuildings = new List<Building>();
            controllingUnits = new List<Unit>();
            score = 0;
            m_designate = designate;

            if (designate == 1)
                color = Color.LightBlue;
            else
                color = Color.Red;

            unitSpecs = new List<UnitSpecs>();
        }

        // Removes all dead units and buildings from the Players lists.
        public int requestUnitID()
        {
            return unitSpawn++;
        }

        public int requestBuildingID()
        {
            return buildingSpawn++;
        }
        public void Update(GameTime gameTime)
        {
            if (gameTime.TotalGameTime >= Game1.RES_TICK + lastResTick)
            {
                lastResTick = gameTime.TotalGameTime;
                m_requisition += reqValue;
            }


            List<Unit> uprune = new List<Unit>();
            foreach (Unit i in controllingUnits)
            {
                if (!i.alive)
                    uprune.Add(i);
                else
                    i.Update(gameTime);
            }

            foreach (Unit u in uprune)
            {
                removeUnit(u);
            }

            List<Building> bprune = new List<Building>();
            foreach (Building i in controllingBuildings)
            {
                hq = (HQBuilding)i; //THIS IS BAD, REMOVE IF THERE ARE OTHER BUILDINGS
                if (!i.alive)
                {
                    bprune.Add(i);
                }

            } //foreach

            foreach (Building b in bprune)
            {
                controllingBuildings.Remove(b);
            }


        }

        public void addUnit(Unit u)
        {
            controllingUnits.Add(u);
            Collision.allUnits.Add(u);
        }

        public void removeUnit(Unit u)
        {
            controllingUnits.Remove(u);
            Collision.allUnits.Remove(u);
        }

        public List<Unit> units
        {
            get { return controllingUnits; }
            set { controllingUnits = value; }
        }
        public List<Building> buildings
        {
            get { return controllingBuildings; }
            set { controllingBuildings = value; }
        }

        public bool alive
        {
            get
            {
                if (buildings.Count <= 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }

            }
        }

        public void draw(SpriteBatch sb)
        {
            if(!alive)
            {
                return;
            }
            foreach (Unit u in units)
            {
                u.Draw(sb);
            }
            foreach (Building b in buildings)
            {
                b.Draw(sb);
            }
        }

        public int requisition
        {
            get { return m_requisition; }
            set { m_requisition = value; }
        }

        public int score
        {
            get { return m_score; }
            set { m_score = value; }
        }

        public int id
        {
            get { return m_designate; }
            set { m_designate = value; }
        }
    }
}
